import { networkInterfaces } from 'os';

export const getLocalhost = () => {
	const interfaces = networkInterfaces();
	for (const name in interfaces) {
		const addresses = interfaces[name];
		for (const address of addresses!) {
			if (address.family === 'IPv4' && !address.internal) {
				return address.address;
			}
		}
	}
	return null;
};

type Color = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'crimson';
export const colorText = (text: string, fontColor: Color, bgColor: Color = 'black') => {
	enum fontColorCode {
		black = '\x1b[30m',
		red = '\x1b[31m',
		green = '\x1b[32m',
		yellow = '\x1b[33m',
		blue = '\x1b[34m',
		magenta = '\x1b[35m',
		cyan = '\x1b[36m',
		white = '\x1b[37m',
		crimson = '\x1b[38m',
	}
	enum bgColorCode {
		black = '\x1b[40m',
		red = '\x1b[41m',
		green = '\x1b[42m',
		yellow = '\x1b[43m',
		blue = '\x1b[44m',
		magenta = '\x1b[45m',
		cyan = '\x1b[46m',
		white = '\x1b[47m',
		crimson = '\x1b[48m',
	}
	const fontCode = fontColorCode[fontColor] || '';
	const bgCode = bgColorCode[bgColor];
	const textCode = `${fontCode}${bgCode}${text}\x1b[0m`;

	return textCode;
};
